package dailyExe.shuzu;

import java.util.TreeMap;

public class 多数元素 {
    public static void main(String[] args) {
        majorityElement(new int[]{3,3,4});
    }

    static int ans = 0;
    static int cent = 0;

    public static int majorityElement(int[] nums) {
        TreeMap<Integer,Integer> map = new TreeMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])) {
                map.put(nums[i], map.get(nums[i]) + 1);
            }else  {
                map.put(nums[i], 1);
            }
        }
        ans = 0;
        cent = 0;
        map.forEach((k,v)->{
           if (cent < v){
               ans = k;
               cent = v;
           }
        });
        return ans;
    }
}
